package edu.kit.ksri.db.mturk.var;

import java.util.Calendar;

import com.amazonaws.mturk.requester.Comparator;
import com.amazonaws.mturk.requester.QualificationRequirement;
import com.amazonaws.mturk.requester.QualificationTypeStatus;

import edu.kit.ksri.db.mturk.plugins.correctness.AbstractQualityControlImplementation;
import edu.kit.ksri.db.mturk.plugins.correctness.SimInputQuality;
import edu.kit.ksri.db.mturk.plugins.correctness.WMVBinomial;
import edu.kit.ksri.db.mturk.plugins.correctness.answers.BasicAssignmentAnswer;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 * 
 * ---------------------------------------------------------------------------
 * 
 * Purpose: For explanation @see edu.kit.ksri.db.mturk.var.Scenario
 */
public class Ocr extends Scenario {

	public static final Ocr INSTANCE = new Ocr();

	private Ocr() {
		
//		-----------------------General----------------------------
		prefix = "Ocr";
		connectionMode = ConnectionMode.LIVE;
		//connectionMode = ConnectionMode.CORRSIM;
		//connectionMode = ConnectionMode.CORRSIMINPUT;
		corrSimModeAssignment = CorrSimMode.RANDOM;
		//corrSimModeAssignment = CorrSimMode.ORDER;
		//corrSimModeHIT = CorrSimMode.RANDOM;
		corrSimModeHIT = CorrSimMode.ORDER;
		simulationHitCount = 8;
		
		timeUntilHITExpiration = 172800L;
		minimumInspectionQualityLevel = 0.99;
		escalationLimit = 0.01;
		i = 6;
		f = 0.2485;
		minimumNumberOfCategories = 3;
		setDeadlineMinutes(5);	
		estimatedTaskDuration = 3500;
		
		basicAssignmentAnswerMode = BasicAssignmentAnswer.RAW_ANSWER_IDENTICAL;
		setCorrectnessPluginClass(WMVBinomial.class);
		answerComparisonClass = BasicAssignmentAnswer.class;

		// configure answerComparisonClassParameters
		answerComparisonClassParameters = new Object[1];
		answerComparisonClassParameters[0] = BasicAssignmentAnswer.RAW_ANSWER_IDENTICAL;
		
//		-----------------------Monitoring----------------------------		
		taskInputType = "IMG";
		taskPictureHeight = "40px";		
		refreshDelay = 1;
		
//		-----------------------Qualification----------------------------
		qualificationName = "Simple OCR qualification";
		qualificationKeywords = "";
		qualificationDescription = "Qualification value is used internally for QM.";
		qualificationTypeStatus = QualificationTypeStatus.Active;
		qualificationRetryDelayInSeconds = 60L;
		qualificationTestDurationInSeconds = 30L;
		qualificationAutoGranted = false;
		qualificationAutoGrantedValue = null;
		//qualificationTypeID = "2OY8B727M0IGOTOIYKPFVVF4VI4CX6"; // Robert
		qualificationTypeID = "2ZS6KW4ZMAZHDD6JMLRORNVRH45H83"; //Jan
		
		// create qualification requirements
		int numberOfRequirements = 0;
		hitQualRequirements = null;
		
		/*
		int numberOfRequirements = 1;
		hitQualRequirements = new QualificationRequirement[numberOfRequirements];
		hitQualRequirements[0] = new QualificationRequirement(
				qualificationTypeID,
				Comparator.GreaterThanOrEqualTo,
				StaticInformation.activePoolQualificationValue, null, false);
		*/
		// add further qualifications by copying and adjusting the last four
		// lines. Also increase the numberOfRequirements and the array index by
		// one. In case no Qualification Test should be applied, then set 
		// hitQualRequirements null.
		
//		-----------------------HIT----------------------------
		hitDescription = "Look at an image, read the word written there (in English) and type it in the box below.";
		hitTitle = "OCR: Type in one word as shown in an image.";
		hitKeywords = "OCR";
		hitReward = 0.01;
		hitAutoApprovalDelayInSeconds = 2592000L;
		hitAssignmentDurationInSeconds = 30L;
		//hitTypeID = "2HQULRGNTBJ3VVK22TG7Q62KHGO7TG"; //Robert OCR with Q-test
		hitTypeID = "24AMVHVKCJ01AT9C1XY9NQW61D175O"; //Robert OCR without Q-test
		hitTypeID = "2JJ45UUKQOYGJ4BZUNZOK010719EWM"; //Jan OCR without Q-test
		hitCreationType = "Manually";
	}
	
	private void setDeadlineMinutes(int m){
		deadline = Calendar.getInstance();
		// deadline is m minutes from now
		deadline.add(Calendar.MINUTE, m);
	}
	
	private void setCorrectnessPluginClass(Class<? extends AbstractQualityControlImplementation>  correctnessPluginClass){
		
		if(connectionMode == ConnectionMode.CORRSIMINPUT)
			this.correctnessPluginClass = SimInputQuality.class;
		else
			this.correctnessPluginClass = correctnessPluginClass;
	}
}
